package com.fuze.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuze.domain.dto.ContractQueryDTO;
import com.fuze.domain.dto.DataGridView;
import com.fuze.domain.dto.SharingContractDTO;
import com.fuze.domain.model.SharingContract;
import com.fuze.mapper.SharingContractMapper;
import com.fuze.service.SharingContractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author sepc_
 * @date 5/19/2021 14:43
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class SharingContractServiceImpl implements SharingContractService {

    final SharingContractMapper sharingContractMapper;

    @Override
    public DataGridView queryByType(ContractQueryDTO queryDTO) {
        Page<SharingContract> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        Page<SharingContract> res = sharingContractMapper.selectPage(page, null);
        return new DataGridView(res.getTotal(), res.getRecords());
    }

    @Override
    public SharingContract findById(Integer id) {
        return this.sharingContractMapper.selectById(id);
    }

    @Override
    public int add(SharingContractDTO sharingContractDTO) {
        SharingContract contract = new SharingContract();
        BeanUtils.copyProperties(sharingContractDTO, contract);
        return this.sharingContractMapper.insert(contract);
    }

    @Override
    public int update(SharingContractDTO sharingContractDTO) {
        SharingContract contract = new SharingContract();
        BeanUtils.copyProperties(sharingContractDTO, contract);
        return this.sharingContractMapper.updateById(contract);
    }

    @Override
    public int delete(Integer[] ids) {
        return this.sharingContractMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
